package com.bjh.netty.codec;

import com.bjh.netty.message.RpcMessage;
import com.bjh.serialization.ProtostuffSerialization;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcEncoder extends MessageToByteEncoder {

    private Class<?> genericClass;

    public RpcEncoder(Class<?> genericClass) {
        this.genericClass = genericClass;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object msg, ByteBuf out) throws Exception {
        byte[] data = ProtostuffSerialization.serialize(msg);
        out.writeInt(data.length);
        out.writeBytes(data);
    }


//    @Override
//    protected void encode(ChannelHandlerContext channelHandlerContext, RpcMessage rpcMessage, ByteBuf out) throws Exception {
//        out.writeInt(rpcMessage.getObj().toString().getBytes().length);
//        out.writeBytes(rpcMessage.getObj().toString().getBytes());
//    }
}
